<?php
/**
 *
 * @package    mahara
 * @subpackage artefact-feedback
 * @author     laurent.opprecht@gmail.com
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2011 University of Geneva http://www.unige.ch/
 *
 */

/**
 * Used to display an html table of feedbacks - with javascript pagination.
 *
 */
class FeedbackTable {

    public static function get_table_id($for) {
        return 'feedbacktable_for_' . $for;
    }
    public static function get_pagination_id($for) {
        return 'feedbackpagination_for_' . $for;
    }

    public static function get_request_url() {
        $r = array();
        foreach ($_REQUEST as $key => $val) {
            if ($key != 'extradata' && $key != 'offset' && $key != 'limit') {
                $r[] = $key . '=' . $val;
            }
        }
        $r = implode('&amp;', $r);
        $result = $_SERVER['PHP_SELF'] . '?' . $r;
        return $result;
    }

    public static function build_rows($parent, $show_private = null, $limit=null, $offset=0) {
        $parent_id = ArtefactTypeFeedback::get_id($parent);
        //$view_id = ArtefactTypeFeedback::get_view_id();
        $limit = ArtefactTypeFeedback::get_limit($limit);

        $data = ArtefactTypeFeedback::get_feedbacks_onparent($parent_id, $show_private, $limit, $offset);
        $table_id = self::get_table_id($parent_id);

        $smarty = smarty();
        $smarty->assign_by_ref('data', $data);
        $smarty->assign('parent_id', $parent_id);
        //$smarty->assign('view_id', $view_id);
        $smarty->assign('table_id', $table_id);
        $result = $smarty->fetch('artefact:feedback:feedback_items.tpl');

        return $result;
    }

    public static function build_pagination($parent_id, $show_private, $count, $limit=null, $offset=0) {
        $pagination = build_pagination(array(
                    'id' => self::get_pagination_id($parent_id),
                    'url' => self::get_request_url(),
                    'jsonscript' => 'artefact/feedback/list.json.php',
                    'datatable' => self::get_table_id($parent_id),
                    'count' => $count,
                    'limit' => $limit,
                    'offset' => $offset,
                    'forceoffset' => null,
                    'resultcounttextsingular' => get_string('Feedback', 'artefact.feedback'),
                    'resultcounttextplural' => get_string('Feedbacks', 'artefact.feedback'),
                    'extradata' => array('parent_id' => $parent_id, 'show_private' => $show_private),
                ));
        return $pagination;
    }
}
?>
